package richbar.com.richbar.fragment.classify;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.common.base.BaseActivity;
import com.common.base.BaseFragment;
import com.common.help.RecycleViewHelp;
import com.common.util.LogUtil;
import com.okhttp.GlideUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.util.ArrayList;
import java.util.List;

import richbar.com.richbar.R;
import richbar.com.richbar.activity.product.ProductListAc;
import richbar.com.richbar.base.BaseListAdapter;
import richbar.com.richbar.bean.CateRightResult;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;
import richbar.com.richbar.view.BannerLayout;
import richbar.com.richbar.view.NoScrollGridView;

/**
 * Created by QiYang on 2019/1/13.
 */

public class BaseNewClassifyFragment extends BaseFragment {
    public static BaseNewClassifyFragment newInstance(String type) {
        BaseNewClassifyFragment fragment = new BaseNewClassifyFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }
    private BannerLayout bannerLayout;
    private RecyclerView recyclerView;
    private String type;//区分是哪个fragment；
    private MyAdapter adapter;
    @Override
    protected View onCreate(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_classify_base, null);
        recyclerView = view.findViewById(R.id.recycleView);
        return view;
    }

    @Override
    protected void initData() {
        type = getArguments().getString("type");
        getData();
    }

    private void getData() {
        NetWorkUtil.getCateRight(Util.md5(), type, new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                LogUtil.e("RRRRRRRRRRR", data);
                final CateRightResult result = MyGson.getInstance().fromJson(data, CateRightResult.class);
                if (checkIsOk(result)) {
                    setData(result);
                }
            }
        });
    }
    private void setData(CateRightResult result) {
        adapter = new MyAdapter(R.layout.item_list_classify, new ArrayList<CateRightResult.DataEntity.CateEntity>());
        RecycleViewHelp.setManager(recyclerView, getActivity());
        recyclerView.setAdapter(adapter);

        View topView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_classify_base_top, null);
        bannerLayout = topView.findViewById(R.id.banner);
        adapter.addHeaderView(topView);
        List<CateRightResult.DataEntity.BannerEntity> banner = result.getData().getBanner();
        if (null != banner && banner.size() > 0) {
            List<String> urls = new ArrayList<>();
            for (int i = 0; i < banner.size(); i++) {
                LogUtil.e("AWDAWDAWDA",NetWorkUtil.URL + banner.get(i).getImg());
                urls.add(NetWorkUtil.URL + banner.get(i).getImg());
            }
            bannerLayout.setViewUrls(urls);
            bannerLayout.setOnBannerChangeListener(new BannerLayout.OnBannerChangeListener() {
                @Override
                public void onChange(int position) {

                }
            });
            bannerLayout.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
                @Override
                public void onItemClick(int position) {

                }
            });
        }

       setRecycleDate(result);
    }

    private void setRecycleDate(CateRightResult result) {
        List<CateRightResult.DataEntity.CateEntity> data = result.getData().getCate();
        LogUtil.e("PPPPPPPPPPPP",data);
        adapter.setNewData(data);
    }


    class MyAdapter extends BaseQuickAdapter<CateRightResult.DataEntity.CateEntity> {
        public MyAdapter(int layoutResId, List<CateRightResult.DataEntity.CateEntity> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder holder, CateRightResult.DataEntity.CateEntity bean) {
            holder.setText(R.id.name, bean.getName());
            NoScrollGridView gridView = holder.getView(R.id.gridView);
            ItemAdapter adapter = new ItemAdapter(getActivity(),bean.getChild());
            gridView.setAdapter(adapter);
        }
    }

    class ItemAdapter extends BaseListAdapter<CateRightResult.DataEntity.CateEntity.ChildEntity> {
        public ItemAdapter(Context context, List<CateRightResult.DataEntity.CateEntity.ChildEntity> data) {
            super(context, data);
        }

        @Override
        public View convert(int i, View view, ViewGroup viewGroup, final CateRightResult.DataEntity.CateEntity.ChildEntity bean) {
            View v = LayoutInflater.from(context).inflate(R.layout.item_classify_child,viewGroup,false);
            TextView name = v.findViewById(R.id.name);
            name.setText(bean.getName());
            ImageView iv = v.findViewById(R.id.iv);
            GlideUtil.loadImage(getActivity(),NetWorkUtil.URL + bean.getImg(),iv);
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    start(ProductListAc.class, new BaseActivity.OnBundleListener() {
                        @Override
                        public Bundle onSetBundle(Bundle bundle) {
                            bundle.putString("goodsName",bean.getName());
                            return bundle;
                        }
                    });
                }
            });
            return v;
        }
    }
}
